<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .box {
            display: flex;
            justify-content: space-around;
            margin-top: 100px;

        }

        body {
            background-color: #ccc;
        }

        .hp {
            background-color: #fff;
            width: 200px;
            height: 30px;
            border: 2px solid #000;
        }

        .hp2 {
            width: 100%;
            background-color: red;
            height: 100%;
            transition: all 1s;
        }
    </style>
    <script>
        var blood = 100

        function gethp() {
            var bloodmath = Math.floor(Math.random() * 30)
            blood = (parseInt(blood) - bloodmath) + '%'
            if (parseInt(blood) <= 0) {
                blood = 0 + '%'
            }
            hp2.style.width = blood
            if (blood == '0%') {
                hp.style.border = '2px soild #f5f5f5'
            }
        }
    </script>
</head>

<body>
    <div class="box">
        <div class="hp">
            <div class="hp2"></div>
        </div>

        <button id="attack" onclick="gethp()">攻击1</button>
    </div>
    <script>
        var hp1 = document.querySelector('.hp1')
        var hp2 = document.querySelector('.hp2')
        var hp = document.querySelector('.hp')
        var attack = document.querySelector('#attack')
    </script>
</body>

</html>